package qlvbdq.bo;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JOptionPane;
import qlvbdq.dao.PhieuBanHangDAO;
import qlvbdq.dto.PhieuBanHangDTO;

/**
 *
 * @author THAI HA
 */
public class PhieuBanHangBO {

    public PhieuBanHangBO() {
    }

    public boolean themPhieuBanHang(PhieuBanHangDTO pbhdto_) {
        boolean result = false;
        Date systemTime = new Date();
        String now = systemTime.toLocaleString().split(" ")[1];
        String timeInPBH = pbhdto_.getNgayBan().toLocaleString().split(" ")[1];
        if (!now.equals(timeInPBH)) {
            result = false;
        } else {
            PhieuBanHangDAO pbhdao = new PhieuBanHangDAO();
            if (pbhdao.InsertPhieuBanHang(pbhdto_) > 0) {
                result = true;
            }
        }
        return result;
    }

    public boolean deletePhieuBanHang(PhieuBanHangDTO pbhdto_) {
        boolean result = false;
        PhieuBanHangDAO pbhdao = new PhieuBanHangDAO();
        if (pbhdao.DeletePhieuBanHang(pbhdto_) > 0) {
            result = true;
        }
        return result;
    }

    public String getNewMaPhieuBanHang() {
        PhieuBanHangDAO pbhdao = new PhieuBanHangDAO();
        try {
            return pbhdao.getNewMaPhieuBanHang();
        } catch (SQLException ex) {
            return null;
        }
    }

    public ArrayList<PhieuBanHangDTO> searchPhieuBanHangByRule(PhieuBanHangDTO pbhdto_) {
        PhieuBanHangDAO pbhdao = new PhieuBanHangDAO();
        try {
            return pbhdao.SearchPhieuBanHangByRule(pbhdto_);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null,
                    "Lỗi thao tác dữ liệu",
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
            return null;
        }
    }
}
